<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcStudentInfoDegree.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateSchool')"
        prop="undergraduateSchool"
      >
        <el-input
          v-model="formData.undergraduateSchool"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateSchool')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateDate')"
        prop="undergraduateDate"
      >
        <el-date-picker
          v-model="formData.undergraduateDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateDate')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateMajor')"
        prop="undergraduateMajor"
      >
        <el-input
          v-model="formData.undergraduateMajor"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateMajor')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateStudyForm')"
        prop="undergraduateStudyForm"
      >
        <el-input
          v-model="formData.undergraduateStudyForm"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.undergraduateStudyForm')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.bachelorUnit')" prop="bachelorUnit">
        <el-input
          v-model="formData.bachelorUnit"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorUnit')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.bachelorDate')" prop="bachelorDate">
        <el-date-picker
          v-model="formData.bachelorDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorDate')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.bachelorMajor')" prop="bachelorMajor">
        <el-input
          v-model="formData.bachelorMajor"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorMajor')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.bachelorCategory')"
        prop="bachelorCategory"
      >
        <el-input
          v-model="formData.bachelorCategory"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorCategory')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateNumber')"
        prop="undergraduateNumber"
      >
        <el-input
          v-model="formData.undergraduateNumber"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateNumber')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateDegree')"
        prop="undergraduateDegree"
      >
        <el-input
          v-model="formData.undergraduateDegree"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateDegree')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.bachelorNumber')" prop="bachelorNumber">
        <el-input
          v-model="formData.bachelorNumber"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorNumber')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.undergraduateQualification')"
        prop="undergraduateQualification"
      >
        <el-input
          v-model="formData.undergraduateQualification"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.undergraduateQualification')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterGraduateUnit')"
        prop="masterGraduateUnit"
      >
        <el-input
          v-model="formData.masterGraduateUnit"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateUnit')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterGraduateDate')"
        prop="masterGraduateDate"
      >
        <el-date-picker
          v-model="formData.masterGraduateDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateDate')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterGraduateMajor')"
        prop="masterGraduateMajor"
      >
        <el-input
          v-model="formData.masterGraduateMajor"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateMajor')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.masterMethod')" prop="masterMethod">
        <el-input
          v-model="formData.masterMethod"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterMethod')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterQualificationUnit')"
        prop="masterQualificationUnit"
      >
        <el-input
          v-model="formData.masterQualificationUnit"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.masterQualificationUnit')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterQualificationDate')"
        prop="masterQualificationDate"
      >
        <el-date-picker
          v-model="formData.masterQualificationDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.masterQualificationDate')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterQualificationMajor')"
        prop="masterQualificationMajor"
      >
        <el-input
          v-model="formData.masterQualificationMajor"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.masterQualificationMajor')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.masterCategory')" prop="masterCategory">
        <el-input
          v-model="formData.masterCategory"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterCategory')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterGraduateNumber')"
        prop="masterGraduateNumber"
      >
        <el-input
          v-model="formData.masterGraduateNumber"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.masterGraduateNumber')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoDegree.fields.masterDegree')" prop="masterDegree">
        <el-input
          v-model="formData.masterDegree"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterDegree')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterQualificationNumber')"
        prop="masterQualificationNumber"
      >
        <el-input
          v-model="formData.masterQualificationNumber"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoDegree.fields.masterQualificationNumber')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoDegree.fields.masterQualification')"
        prop="masterQualification"
      >
        <el-input
          v-model="formData.masterQualification"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterQualification')
          "
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UstcStudentInfoDegreeApi, UstcStudentInfoDegreeVO } from '@/api/core/ustcstudentinfodegree'
import { FormRules } from 'element-plus'

/** 学生本科/硕士信息 表单 */
defineOptions({ name: 'StudentInfoDegreeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  undergraduateSchool: undefined,
  undergraduateDate: undefined,
  undergraduateMajor: undefined,
  undergraduateStudyForm: undefined,
  bachelorUnit: undefined,
  bachelorDate: undefined,
  bachelorMajor: undefined,
  bachelorCategory: undefined,
  undergraduateNumber: undefined,
  undergraduateDegree: undefined,
  bachelorNumber: undefined,
  undergraduateQualification: undefined,
  masterGraduateUnit: undefined,
  masterGraduateDate: undefined,
  masterGraduateMajor: undefined,
  masterMethod: undefined,
  masterQualificationUnit: undefined,
  masterQualificationDate: undefined,
  masterQualificationMajor: undefined,
  masterCategory: undefined,
  masterGraduateNumber: undefined,
  masterDegree: undefined,
  masterQualificationNumber: undefined,
  masterQualification: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.accountId'),
      trigger: 'blur'
    }
  ],
  undergraduateSchool: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateSchool'),
      trigger: 'blur'
    }
  ],
  undergraduateDate: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateDate'),
      trigger: 'blur'
    }
  ],
  undergraduateMajor: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateMajor'),
      trigger: 'blur'
    }
  ],
  undergraduateStudyForm: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateStudyForm'),
      trigger: 'blur'
    }
  ],
  bachelorUnit: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorUnit'),
      trigger: 'blur'
    }
  ],
  bachelorDate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorDate'),
      trigger: 'blur'
    }
  ],
  bachelorMajor: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorMajor'),
      trigger: 'blur'
    }
  ],
  bachelorCategory: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorCategory'),
      trigger: 'blur'
    }
  ],
  undergraduateNumber: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateNumber'),
      trigger: 'blur'
    }
  ],
  undergraduateDegree: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.undergraduateDegree'),
      trigger: 'blur'
    }
  ],
  bachelorNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.bachelorNumber'),
      trigger: 'blur'
    }
  ],
  undergraduateQualification: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoDegree.fields.undergraduateQualification'),
      trigger: 'blur'
    }
  ],
  masterGraduateUnit: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateUnit'),
      trigger: 'blur'
    }
  ],
  masterGraduateDate: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateDate'),
      trigger: 'blur'
    }
  ],
  masterGraduateMajor: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateMajor'),
      trigger: 'blur'
    }
  ],
  masterMethod: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterMethod'),
      trigger: 'blur'
    }
  ],
  masterQualificationUnit: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoDegree.fields.masterQualificationUnit'),
      trigger: 'blur'
    }
  ],
  masterQualificationDate: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoDegree.fields.masterQualificationDate'),
      trigger: 'blur'
    }
  ],
  masterQualificationMajor: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoDegree.fields.masterQualificationMajor'),
      trigger: 'blur'
    }
  ],
  masterCategory: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterCategory'),
      trigger: 'blur'
    }
  ],
  masterGraduateNumber: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterGraduateNumber'),
      trigger: 'blur'
    }
  ],
  masterDegree: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterDegree'),
      trigger: 'blur'
    }
  ],
  masterQualificationNumber: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoDegree.fields.masterQualificationNumber'),
      trigger: 'blur'
    }
  ],
  masterQualification: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.masterQualification'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoDegree.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentInfoDegreeApi.getUstcStudentInfoDegree(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentInfoDegreeVO
    if (formType.value === 'create') {
      await UstcStudentInfoDegreeApi.createUstcStudentInfoDegree(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentInfoDegreeApi.updateUstcStudentInfoDegree(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    undergraduateSchool: undefined,
    undergraduateDate: undefined,
    undergraduateMajor: undefined,
    undergraduateStudyForm: undefined,
    bachelorUnit: undefined,
    bachelorDate: undefined,
    bachelorMajor: undefined,
    bachelorCategory: undefined,
    undergraduateNumber: undefined,
    undergraduateDegree: undefined,
    bachelorNumber: undefined,
    undergraduateQualification: undefined,
    masterGraduateUnit: undefined,
    masterGraduateDate: undefined,
    masterGraduateMajor: undefined,
    masterMethod: undefined,
    masterQualificationUnit: undefined,
    masterQualificationDate: undefined,
    masterQualificationMajor: undefined,
    masterCategory: undefined,
    masterGraduateNumber: undefined,
    masterDegree: undefined,
    masterQualificationNumber: undefined,
    masterQualification: undefined
  }
  formRef.value?.resetFields()
}
</script>
